package com.tamplus.cs.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.faces.context.FacesContext;

import com.tamplus.cs.constant.Constant;
import com.tamplus.cs.constant.MessageConstant;
import com.tamplus.cs.exception.CSException;
import com.tamplus.cs.helper.MessageHelper;
import com.tamplus.cs.model.CheckupPackage;
import com.tamplus.cs.model.Patient;
import com.tamplus.cs.service.CallService;

public class SearchBean extends BaseBean implements Serializable {
	private static final long serialVersionUID = 1L;
	
	private String hn;
	
	private Patient patient;
	private List<CheckupPackage> items;

	public void init() {
		loadData();
		this.items = new ArrayList<CheckupPackage>();
	}

	/**
	 * Load all data
	 */
	private void loadData() {
		FacesContext context = FacesContext.getCurrentInstance();
		System.out.println("Clear Session Success!!");
		if (context.getExternalContext().getSessionMap().containsKey("loginBean")) {
			context.getExternalContext().getSessionMap().remove("loginBean");
		}
	}
	
	public static void main(String[] args){
		SearchBean bean = new SearchBean();
		bean.search();
	}
	
	/**
	 * Clear session of search
	 */
	private void clearBean() {
		FacesContext context = FacesContext.getCurrentInstance();
		System.out.println("[SearchBean.clearBean] Clear Session Success!!");
		if (context.getExternalContext().getSessionMap().containsKey("packageBean")) {
			context.getExternalContext().getSessionMap().remove("packageBean");
		}
	}
	
	/**
	 * Search fund
	 * 
	 * @return
	 */
	public String search() {
		setPage(1);
		clearBean();
		
		try{
			this.items = new ArrayList<CheckupPackage>();
			
			//call web service for get patient info and flag
			CallService service = new CallService();
			if(patient != null){
				patient.setTitle("");
				patient.setName("");
				patient.setSurname("");
			}
			patient = service.getPatientProfile(hn);
			
			this.items = patient.getPackageList();

			//TODO fix for test
	//		Patient patient = new Patient();
	//		patient.setHn("12345");
	//		patient.setTitle("นาย");
	//		patient.setName("Name");
	//		patient.setSurname("SurName");
	//		patient.setHnFlag("Y");
	//		patient.setCheckupFlag("Y");
	//		this.patient = patient;
	//		for(int i=0; i<15; i++){
	//			CheckupPackage item = new CheckupPackage();
	//			item.setVisitDate(new Date());
	//			item.setPackageName("Package Name"+(i+1));
	//			item.setAmount(100*i);
	//			item.setStatus("Buy");
	//			this.items.add(item);
	//		}
			
			displayInfoMessage(MessageConstant.SEARCH_SUCCESS, new String[] { hn });
		}catch(CSException e){
			if(patient == null) patient = new Patient();
			patient.setHn(hn);
			patient.setHnFlag(Constant.FLAG_N);
			patient.setCheckupFlag(Constant.FLAG_N);
			FacesContext context = FacesContext.getCurrentInstance();
			String error = MessageHelper.getLocaleString(context.getApplication().getMessageBundle(), MessageConstant.SEARCH_FAIL, null, Locale.US);
			setErrorMessage(error);
		}catch(Exception e){
//			displayErrorMessage(MessageConstant.SEARCH_FAIL, new String[] { hn });	
			setErrorMessage(e.getMessage());
		}
		
		return FWD_SUCCESS;
	}

	public String getHn() {
		return hn;
	}

	public void setHn(String hn) {
		this.hn = hn;
	}

	public List<CheckupPackage> getItems() {
		return items;
	}

	public void setItems(List<CheckupPackage> items) {
		this.items = items;
	}

	public Patient getPatient() {
		return patient;
	}

	public void setPatient(Patient patient) {
		this.patient = patient;
	}

}
